package person.csx.studentgradle.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import person.csx.studentgradle.model.Student;
import person.csx.studentgradle.service.StudentService;

import java.util.List;

//@Controller的作用是生成了一个servlet的对象，名字叫studentController

@Controller
//@RequestMapping的作用是在url请求与类或方法之间建立对应关系
@RequestMapping("/Student")
public class StudentController {
    //自动注入service层的对象studentServiceImpl
    @Autowired
    //如果spring容器里只有一个StudentService的对象，可以省略@Qualifier("")标识,alt+enter
    private StudentService studentServiceImpl;

    @RequestMapping("list.do/{currPage}/{pageSize}")
    //@PathVariable从路径上获取参数值
    public String listStudents(Model model, @PathVariable("currPage") int currPage,@PathVariable("pageSize") int pageSize) {
       // List<Student> students = studentServiceImpl.list();
        //调用service的分页查询方法
        List<Student> students = studentServiceImpl.selectByPage(currPage,pageSize);
//        //传出students对象
        model.addAttribute("students", students);
        //转向到listStudents.html页面
        return "listStudents";
    }

    @RequestMapping("toAdd.do")
    public String toAdd() {
        //转向到listStudents.html页面
        return "add";
    }

    //    @RequestMapping(value = "add.do")
//    //定义响应给用户的是数据
//   // @ResponseBody
//    public String addStudent(Student student, Model model, HttpSession session, HttpServletResponse res){
//        //创建cookie,username_cookie,password_cookie
//        Cookie username_cookie=new Cookie("username",student.getName());
//   //     Cookie password_cookie=new Cookie("password",student.getPwd());
//        //从服务器往客户端写cookie
//        res.addCookie(username_cookie);
//    //    res.addCookie(password_cookie);
//        //设置会话有效的变量username,变量的生命周期有四种范围，从小到大是page,request,session,application
//        session.setAttribute("username",student.getName());
//
//    //在控制台打印用户输入的变量值
//        model.addAttribute("student",student);
//    //    System.out.println("username:"+student.getName()+",password:"+student.getPwd()+",age is"+student.getAge());
//        //转向到listStudents.html页面
//        return "listStudents";
//    }
    @RequestMapping(value = "add.do")
    public String addStudent(Student student) {
        studentServiceImpl.insert(student);
        return "redirect:list.do";
    }

    //转向任意页面，由参数page定义
    @RequestMapping("getPage.do")
    public String getPage(String page) {
        return page;
    }

    //删除，转向到listStudents.html页面
    @RequestMapping("delete.do")
    public String delete(int id) {
        studentServiceImpl.delete(id);

        return "redirect:list.do";
    }
   @RequestMapping("checkOnlyByName")
   @ResponseBody
    public String checkOnlyByName(String name){
        return  (studentServiceImpl.checkOnlyByName(name)==0)?"用户名可以使用":"用户名已存在，请使用其他用户名";
   }

   @RequestMapping("checkOnlyById")
    @ResponseBody
    public String checkOnlyById(String id){
        return  (studentServiceImpl.checkOnlyById(id)==0)?"学号可以使用":"学号已存在";
//       return (studentServiceImpl.checkOnlyById(id));
    }

}
//entity,domain